<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                suggestRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#suggestGird").jqGrid('getGridParam', 'selrow');
                        var suggest = jQuery("#suggestGird").jqGrid('getRowData', rowid);
                        dijit.byId('suggestUFD').setValues(suggest);
                    }
                };
                topic.subscribe("suggest/insertOrUpdate", suggestRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#suggestGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#suggestGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#suggestGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#suggestGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                suggestSubmitForm = function(formId, type) {
                    var url = "suggest/insert";
                    if (type == 'u') {
                        url = "suggest/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("suggest/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                suggestDelete = function() {
                    var ids = jQuery("#suggestGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var suggest = {};
                            suggest.id = '';
                            array.forEach(ids, function(entry, i) {
                                suggest.id += entry + ",";
                            });
                            syncDB("suggest/delete", JSON.stringify(suggest));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                suggestQuery = function(formId) {
                    var suggest = dijit.byId(formId).getValues();
                    jQuery("#suggestGird").jqGrid('setGridParam', {
                        postData : suggest,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="suggestIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table border="0">
     <tr>
        <td><label for="senderfullname">发送人名: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="senderfullname"></td>
        <td><label for="phone">联系电话: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="phone"></td>
      </tr>
      <tr>
        <td><label for="email">Email: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="email" style="width: 99.5%"></td>
      </tr>
      <tr>
        <td><label for="subject">意见标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subject"></td>
        <td><label for="createtime">创建日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="createtime" onClick="WdatePicker()"></td>
      </tr>
      <tr>
        <td><label for="content">意见内容: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/SimpleTextarea" type="text" rows=3 cols=48
          name="content"></td>
      </tr>
      <tr>
        <td><label for="isopen">是否公开: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="isopen"></td>
        <td><label for="status">状态: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="status"></td>
      </tr>
      <tr>
        <td><label for="querypwd">密码: </label></td>
        <td colspan="3"><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="querypwd"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="suggestSubmitForm('suggestIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="suggestRestForm('suggestIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="suggestUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="boxid" style="display: none;">
    <table>
      <tr>
        <td><label for="subject">意见标题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="subject"></td>
      </tr>
      <tr>
        <td><label for="content">意见内容: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="content"></td>
      </tr>
      <tr>
        <td><label for="createtime">创建日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="createtime"></td>
      </tr>
      <tr>
        <td><label for="recuid">接收人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="recuid"></td>
      </tr>
      <tr>
        <td><label for="recfullname">接收人名: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="recfullname"></td>
      </tr>
      <tr>
        <td><label for="senderid">发送人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="senderid"></td>
      </tr>
      <tr>
        <td><label for="senderfullname">发送人名: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="senderfullname"></td>
      </tr>
      <tr>
        <td><label for="senderip">发送人IP: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="senderip"></td>
      </tr>
      <tr>
        <td><label for="phone">联系电话: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="phone"></td>
      </tr>
      <tr>
        <td><label for="email">Email: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="email"></td>
      </tr>
      <tr>
        <td><label for="isopen">是否公开: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="isopen"></td>
      </tr>
      <tr>
        <td><label for="replycontent">回复内容: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="replycontent"></td>
      </tr>
      <tr>
        <td><label for="replytime">回复时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="replytime"></td>
      </tr>
      <tr>
        <td><label for="replyid">回复人ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="replyid"></td>
      </tr>
      <tr>
        <td><label for="replyfullname">回复人名: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="replyfullname"></td>
      </tr>
      <tr>
        <td><label for="status">状态: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="status"></td>
      </tr>
      <tr>
        <td><label for="querypwd">密码: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="querypwd"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="suggestSubmitForm('suggestUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="suggestRestForm('suggestUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="querySuggestForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td width="8%"><label for="subject">意见标题: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="subject"></td>
          <td width="8%"><label for="createtime">创建日期: </label></td>
          <td width="16%">
          <input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="createtime" onClick="WdatePicker()" style="width: 40%">—<input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="exptime" onClick="WdatePicker()" style="width: 40%">
          </td>
          <td width="8%"><label for="senderfullname">发送人: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="senderfullname"></td>
        </tr>
        <tr>
          <td width="8%"><label for="senderip">发送人IP: </label></td>
          <td width="16%"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="senderip"></td>
          <td width="8%"><label for="isopen">是否公开: </label></td>
          <td width="16%">
          <select data-dojo-type="dijit/form/Select" name="isopen" style="width: 86%">
              <option value="">全部</option>
              <option value="0">不公开</option>
              <option value="1">公开</option>
            </select>
          </td>
          <td width="8%"><label for="status">状态: </label></td>
          <td width="16%">
          <select data-dojo-type="dijit/form/Select" name="status" style="width: 73%">
              <option value="">全部</option>
              <option value="0">禁用</option>
              <option value="1">激活</option>
            </select>
          </td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="suggestInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('suggestIFD').show();
      </script>
    </button>
    <button id="suggestDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="suggestDelete();"></button>
    <button id="suggestUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#suggestGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var suggest = jQuery("#suggestGird").jqGrid('getRowData', ids[0]);
          dijit.byId('suggestUFD').setValues(suggest);
          dijit.byId('suggestUFD').show();
        }
      </script>
    </button>
    <button id="suggestQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="suggestQuery('querySuggestForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="suggestGird"></table>
  <div id="suggestToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#suggestGird").jqGrid({
        url : 'suggest/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '意见箱ID',
          '意见标题',
          '意见内容',
          '创建日期',
          '接收人ID',
          '接收人名',
          '发送人ID',
          '发送人名',
          '发送人IP',
          '联系电话',
          'Email',
          '是否公开',
          '回复内容',
          '回复时间',
          '回复人ID',
          '回复人名',
          '状态',
          '密码'
        ],
        colModel : [ 
        {
          name : 'boxid',
          index : 'boxid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'subject',
          index : 'subject',
          width : 120
        },
        {
          name : 'content',
          index : 'content',
          hidden : true
        },
        {
          name : 'createtime',
          index : 'createtime',
          width : 100
        },
        {
          name : 'recuid',
          index : 'recuid',
          hidden : true
        },
        {
          name : 'recfullname',
          index : 'recfullname',
          width : 80
        },
        {
          name : 'senderid',
          index : 'senderid',
          hidden : true
        },
        {
          name : 'senderfullname',
          index : 'senderfullname',
          width : 80
        },
        {
          name : 'senderip',
          index : 'senderip',
          hidden : true
        },
        {
          name : 'phone',
          index : 'phone',
          width : 100
        },
        {
          name : 'email',
          index : 'email',
          width : 120
        },
        {
          name : 'isopen',
          index : 'isopen',
          hidden : true
        },
        {
          name : 'replycontent',
          index : 'replycontent',
          hidden : true
        },
        {
          name : 'replytime',
          index : 'replytime',
          width : 100
        },
        {
          name : 'replyid',
          index : 'replyid',
          hidden : true
        },
        {
          name : 'replyfullname',
          index : 'replyfullname',
          width : 80
        },
        {
          name : 'status',
          index : 'status',
          width : 60
        },
        {
          name : 'querypwd',
          index : 'querypwd',
          hidden : true
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#suggestToolbar',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "意见箱列表"
    });
    jQuery("#suggestGird").jqGrid('navGrid', '#suggestToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
